/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.saville.testhmc;

import com.saville.debug.*;
import com.saville.hmc.*;
import com.saville.msgcomp.*;

// import java.lang.Exception;

public class TestDeferredMsgs extends ActiveHmc {
    public TestDeferredMsgs(String name) {
        super(name, 4, 0);
        df = Log.VERBOSE;

        states[0] = new State_HandleStarted();
        states[1] = new State_HandleDefer1();
        states[2] = new State_HandleDefer2();
        states[3] = new State_HandleDefer3();

        expectedCmd = McConst.MC_CMD_STARTED;

        /*
         * C A R E F U L: code after the call to completeConstruction 
         * may be executed after MC_CMD_STARTED is delivered to the
         * initial state.
         *
         * For instance I had expectedCmd initialized after the call to
         * completeConstruction and an error occurred!
         */
        completeConstruction();
        Log.d(df, "%s: TestDeferredMsgs() X", mName);
    }

    private class State_HandleStarted extends State {
        @Override
        public boolean doState(Msg msg) {
            Log.d(df, "%s: HandleStarted E expectedCmd=%d msg.cmd=%d", mName, expectedCmd, msg.cmd);

            if (msg.cmd == expectedCmd) {
                if (msg.is(McConst.MC_GUID, McConst.MC_CMD_STARTED)) {
                    /*
                     * Send a message to ourselves, that will be processed here
                     * because we won't processes deferred messages until we
                     * transition which we won't do until we handle this
                     * message.
                     */
                    Msg m = getMsgForSelf(0, -1);
                    sendMsg(m);
                    expectedCmd = -1;

                    /*
                     * Defer two new messages These will be processed after we
                     * transition.
                     */
                    m = getMsgForSelf(0, 2);
                    Log.d(df, "%s: Defer msg 2", mName);
                    deferMsg(m);
                    m = getMsgForSelf(0, 1);
                    Log.d(df, "%s: Defer msg 1", mName);
                    deferMsg(m);

                } else if (msg.cmd == -1) {
                    /*
                     * Send a message to ourselves, This will be processed AFTER
                     * the deferred messages as we ARE going to transition.
                     */
                    Msg m = getMsgForSelf(0, 3);
                    sendMsg(m);

                    /*
                     * Now transition and we will processes the deferred msgs
                     * with the first deferred to be the first process.
                     */
                    expectedCmd = 2;

                    transitionTo(1);
                } else {
                    throw new RuntimeException("Unexpected msg 0.1");
                }
            } else {
                throw new RuntimeException("Unexpected msg 0.2");
            }

            Log.d(df, "%s: HandleStarted X msg.cmd=%d", mName, msg.cmd);
            return true;
        }
    }

    private class State_HandleDefer1 extends State {
        @Override
        public boolean doState(Msg msg) {
            Log.d(df, "%s: HandleDefer1 E expectedCmd=%d msg.cmd=%d", mName, expectedCmd, msg.cmd);

            if (msg.cmd == expectedCmd) {
                /*
                 * Expect cmd 2 then cmd 1
                 */
                if (msg.cmd == 2) {
                    expectedCmd = 1;
                    deferMsg(dupMsg(msg));
                } else if (msg.cmd == 1) {
                    expectedCmd = 2;
                    transitionTo(2);
                } else {
                    throw new RuntimeException("Unexpected msg 1.1");
                }
            } else {
                throw new RuntimeException("Unexpected msg 1.2");
            }

            Log.d(df, "%s: HandleDefer1 X msg.cmd=%d", mName, msg.cmd);
            return true;
        }
    }

    private class State_HandleDefer2 extends State {
        @Override
        public boolean doState(Msg msg) {
            Log.d(df, "%s: HandleDefer2 E expectedCmd=%d msg.cmd=%d", mName, expectedCmd, msg.cmd);

            if (msg.cmd == expectedCmd) {
                if (msg.cmd == 2) {
                    expectedCmd = 3;
                    transitionTo(3);
                } else {
                    throw new RuntimeException("Unexpected msg 2.1");
                }
            } else {
                throw new RuntimeException("Unexpected msg 2.2");
            }

            Log.d(df, "%s: HandleDefer2 X msg.cmd=%d", mName, msg.cmd);
            return true;
        }
    }

    private class State_HandleDefer3 extends State {
        @Override
        public boolean doState(Msg msg) {
            Log.d(df, "%s: HandleDefer3 E expectedCmd=%d msg.cmd=%d", mName, expectedCmd, msg.cmd);

            if (msg.cmd == expectedCmd) {
                if (msg.cmd == 3) {
                    expectedCmd = -1;
                    transitionTo(0);
                } else {
                    throw new RuntimeException("Unexpected msg 3.1");
                }
            } else {
                throw new RuntimeException("Unexpected msg 3.2");
            }

            Log.d(df, "%s: Handle3 X msg.cmd=%d", mName, msg.cmd);
            return true;
        }
    }

    private int expectedCmd;
}
